package tk.redbluelotto.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.mvel2.MVEL;

import tk.redbluelotto.model.RedBall;

/**
 * 给定表达式判断是否为真 red1,red2,red3,red4,red5,red6,B1 分别表示红球1，2，3，4，5，6，蓝球1 e.g. R1%3=2 && R2%2=1
 * && (red3-red2) ==1
 * 
 * @author zb
 * 
 */
public class ArithmeticRule implements IRule {

	private List<Serializable> compiled = new ArrayList<Serializable>();

	public ArithmeticRule() {
	}

	public ArithmeticRule(String ruleExpress) {
		init(ruleExpress);
	}

	/*
	 * @see com.rbl.IRule#init(java.lang.String)
	 */
	@Override
	public void init(String ruleExpress) {
		compiled.add(MVEL.compileExpression(ruleExpress));
	}

	public void addPreQueryRule(Serializable compiler) {
		compiled.add(compiler);
	}

	@Override
	public boolean isSatisfied(RedBall number) {
		for (Serializable compiler : compiled) {
			if (!(Boolean) MVEL.executeExpression(compiler, number))
				return false;
		}
		return true;
	}

}
